﻿using Application.Common.Interfaces;
using AutoMapper;
using Common.Attributes;
using Common.Enums;
using Domain.Entities.Identity;
using Domain.ValueObjects;
using MediatR;
using System.ComponentModel;

namespace Application.Features.Menus.Commands.Add;

/// <summary>
/// 创建菜单
/// </summary>
[Map(typeof(Menu))]
[Description("创建")]
[GroupDescription("菜单管理")]
public class AddMenuCommand : IRequest<bool>
{
    /// <summary>
    /// 父级节点
    /// </summary>
    [Description("父级节点")]
    public long? ParentId { get; set; }
    /// <summary>
    /// 路由名称
    /// </summary>
    [Description("路由名称")]
    public  string? Name { get; set; }

    /// <summary>
    /// 路由路径
    /// </summary>
    [Description("路由路径")]
    public  string? Path { get; set; }

    /// <summary>
    /// 路由重定向
    /// </summary>
    [Description("路由重定向")]
    public  string? Redirect { get; set; }

    /// <summary>
    /// 组件路径
    /// </summary>
    [Description("组件路径")]
    public  string? Component { get; set; }

    /// <summary>
    /// 权限标识
    /// </summary>
    [Description("权限标识")]
    public  string? Code { get; set; }

    /// <summary>
    /// 链接地址
    /// </summary>
    [Description("链接地址")]
    public  string? Url { get; set; }

    /// <summary>
    /// 接口请求方式
    /// </summary>
    [Description("接口请求方式")]
    public  string? HttpMethods { get; set; }

    /// <summary>
    /// 菜单名称
    /// </summary>
    [Description("菜单名称")]
    public string? Title { get; set; }
    /// <summary>
    /// 图标
    /// </summary>
    [Description("图标")]
    public string? Icon { get; set; }
    /// <summary>
    /// 菜单高亮
    /// </summary>
    [Description("菜单高亮")]
    public string? Active { get;  set; }
    /// <summary>
    /// 颜色
    /// </summary>
    [Description("颜色")]
    public string? Color { get;  set; }
    /// <summary>
    /// 类型
    /// </summary>
    [Description("类型")]
    public MetaType? MenuType { get;  set; }
    /// <summary>
    /// 整页路由
    /// </summary>
    [Description("整页路由")]
    public bool? Fullpage { get;  set; }
    /// <summary>
    /// 隐藏
    /// </summary>
    [Description("隐藏")]
    public bool? Hidden { get;  set; }
    /// <summary>
    /// 隐藏面包屑
    /// </summary>
    [Description("隐藏面包屑")]
    public bool? HiddenBreadcrumb { get;  set; }
    /// <summary>
    /// 标签
    /// </summary>
    [Description("标签")]
    public string? Tag { get;  set; }

    /// <summary>
    /// 排序
    /// </summary>
    [Description("排序")]
    public int Rank { get; set; } = 0;
}
/// <summary>
/// 处理程序
/// </summary>
public class AddMenuCommandHandler : IRequestHandler<AddMenuCommand, bool>
{
    private readonly IApplicationDbContext _context;
    private readonly IMapper _mapper;

    public AddMenuCommandHandler(
        IApplicationDbContext context,
        IMapper mapper)
    {
        _context = context;
        _mapper = mapper;
    }

    /// <summary>
    /// 业务逻辑
    /// </summary>
    /// <param name="request">请求参数</param>
    /// <param name="cancellationToken">取消标记</param>
    /// <returns>返回处理结果</returns>
    public async Task<bool> Handle(AddMenuCommand request, CancellationToken cancellationToken)
    {
        if (string.IsNullOrWhiteSpace(request.Name))
            request.Name = request.Title;

        var menuToAdd = _mapper.Map<Menu>(request);
        menuToAdd.Meta = new Meta(
            request.MenuType,
            request.Title,
            request.Icon,
            request.Active,
            request.Color,
            request.Fullpage,
            request.Hidden,
            request.HiddenBreadcrumb,
            request.Tag,
            request.Rank);

        await _context.Menus.AddAsync(menuToAdd);

        return true;
    }
}